HAL UART types migration

This section provides information about the HAL UART types migration .

HAL UART enumeration migration

HAL UART callback ID type definition

HAL1

HAL2

typedef enum
{
    HAL_UART_TX_HALFCOMPLETE_CB_ID,
    HAL_UART_TX_COMPLETE_CB_ID,
    HAL_UART_RX_HALFCOMPLETE_CB_ID,
    HAL_UART_RX_COMPLETE_CB_ID,
    HAL_UART_ERROR_CB_ID,
    HAL_UART_ABORT_COMPLETE_CB_ID,
    HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID,
    HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID,
    HAL_UART_WAKEUP_CB_ID,
    HAL_UART_RX_FIFO_FULL_CB_ID,
    HAL_UART_TX_FIFO_EMPTY_CB_ID,
    HAL_UART_MSPINIT_CB_ID,
    HAL_UART_MSPDEINIT_CB_ID
} HAL_UART_CallbackIDTypeDef;

Removed.

Note

The callback registration in HAL2 is handled by specific functions for each UART event. See Register callbacks for more details.

HAL_UART_MSPINIT_CB_ID and HAL_UART_MSPDEINIT_CB_ID are removed because the functions HAL_UART_MspInit() and HAL_UART_MspDeInit() are removed.

Related concept:


HAL UART clock source type definition

HAL1

HAL2

Example

typedef enum
{
  UART_CLOCKSOURCE_PCLK1,
  UART_CLOCKSOURCE_PCLK2,
  UART_CLOCKSOURCE_HSI,
  UART_CLOCKSOURCE_SYSCLK,
  UART_CLOCKSOURCE_LSE,
  UART_CLOCKSOURCE_UNDEFINED
} UART_ClockSourceTypeDef;

Removed: In HAL1, this structure was used internally for baudrate calculations and was not exposed to the user.


HAL UART states type definition

In HAL2, key changes include:
  1. Renaming of several states.

  2. Removal of states HAL_UART_STATE_TIMEOUT and HAL_UART_STATE_ERROR.

  3. Adding a new state HAL_UART_STATE_ABORT

  4. Refactoring of the state machine into three distinct state machines:
    • Global state machine: hal_uart_state_t

    • Reception state machine: hal_uart_rx_state_t

    • Transmission state machine: hal_uart_tx_state_t

HAL1

HAL2

typedef enum
{
  HAL_UART_STATE_RESET,
  HAL_UART_STATE_READY,
  HAL_UART_STATE_BUSY,
  HAL_UART_STATE_BUSY_TX,
  HAL_UART_STATE_BUSY_RX,
  HAL_UART_STATE_BUSY_TX_RX
  HAL_UART_STATE_TIMEOUT,
  HAL_UART_STATE_ERROR
} HAL_UART_StateTypeDef;
typedef enum
{
  HAL_UART_STATE_RESET,
  HAL_UART_STATE_INIT,
  HAL_UART_STATE_CONFIGURED
} hal_uart_state_t;

typedef enum
{
  HAL_UART_RX_STATE_RESET,
  HAL_UART_RX_STATE_IDLE,
  HAL_UART_RX_STATE_ACTIVE,
  HAL_UART_RX_STATE_ABORT
} hal_uart_rx_state_t;

typedef enum
{
  HAL_UART_TX_STATE_RESET,
  HAL_UART_TX_STATE_IDLE,
  HAL_UART_TX_STATE_ACTIVE,
  HAL_UART_TX_STATE_ABORT
} hal_uart_tx_state_t;

HAL UART structure migration

HAL UART handle type definition

HAL1

HAL2

UART_HandleTypeDef

hal_uart_handle_t

Related concept:


HAL UART initialization type definition

HAL1

HAL2

typedef struct
{
    uint32_t BaudRate;
    uint32_t WordLength;
    uint32_t StopBits;
    uint32_t Parity;
    uint32_t Mode;
    uint32_t HwFlowCtl;
    uint32_t OverSampling;
    uint32_t OneBitSampling;
    uint32_t ClockPrescaler;
} UART_InitTypeDef;
typedef struct
{
    uint32_t baud_rate;
    hal_uart_word_length_t word_length;
    hal_uart_stop_bits_t stop_bits;
    hal_uart_parity_t parity;
    hal_uart_direction_t direction;
    hal_uart_hw_control_t hw_flow_ctl;
    hal_uart_oversampling_t oversampling;
    hal_uart_one_bit_sample_t one_bit_sampling;
    hal_uart_prescaler_t clock_prescaler;
} hal_uart_config_t;

Related concept: Replaced TypeDef suffix with _t in type names


HAL UART advanced feature initialization type definition

HAL1

HAL2

typedef struct
{
    uint32_t AdvFeatureInit;
    uint32_t TxPinLevelInvert;
    uint32_t RxPinLevelInvert;
    uint32_t DataInvert;
    uint32_t Swap;
    uint32_t OverrunDisable;
    uint32_t DMADisableonRxError;
    uint32_t AutoBaudRateEnable;
    uint32_t AutoBaudRateMode;
    uint32_t MSBFirst;
} UART_AdvFeatureInitTypeDef;

This structure has been removed, along with its association as a member of the handle.

Each advanced feature is now configured and controlled through specific functions, allowing direct management of these features without relying on the previous structure.

For more information on enabling or disabling advanced features, see the section HAL UART enable/disable advanced features for more details on advanced feature management functions.

Usage Sequence

HAL1 Usage

HAL2 Usage

UART_HandleTypeDef huart1;


// Add your basic config to the handle

huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT |
                                     UART_ADVFEATURE_RXINVERT_INIT |
                                     UART_ADVFEATURE_DATAINVERT_INIT |
                                     UART_ADVFEATURE_SWAP_INIT |
                                     UART_ADVFEATURE_RXOVERRUNDISABLE_INIT |
                                     UART_ADVFEATURE_DMADISABLEONERROR_INIT |
                                     UART_ADVFEATURE_MSBFIRST_INIT |
                                     UART_ADVFEATURE_AUTOBAUDRATE_INIT;

huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
huart1.AdvancedInit.RxPinLevelInvert = UART_ADVFEATURE_RXINV_ENABLE;
huart1.AdvancedInit.DataInvert = UART_ADVFEATURE_DATAINV_ENABLE;
huart1.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE;
huart1.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
huart1.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
huart1.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
huart1.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;
huart1.AdvancedInit.MSBFirst = UART_ADVFEATURE_MSBFIRST_ENABLE;

/* Initialization and configure basic and advanced configuration features selected  */
HAL_UART_Init(&huart1);
hal_uart_handle_t hUART1;
hal_uart_config_t uart_config;

// Add your basic config

/* Initialization */
HAL_UART_Init(&hUART1,HAL_UART1);

/* Basic configuration */
HAL_UART_SetConfig(&hUART1, &uart_config);



/* Configure and enable advanced features */
HAL_UART_EnableTxPinLevelInvert(&hUART1);
HAL_UART_EnableRxPinLevelInvert(&hUART1);
HAL_UART_EnableDataInvert(&hUART1);
HAL_UART_EnableTxRxSwap(&hUART1);
HAL_UART_DisableRxOverRunDetection(&hUART1);
HAL_UART_EnableDMAStopOnRxError(&hUART1);
HAL_UART_SetConfigAutoBaudRateMode(&hUART1, HAL_UART_AUTO_BAUD_DET_ON_START_BIT);
HAL_UART_EnableAutoBaudRate(&hUART1);
HAL_UART_EnableMSBFirst(&hUART1);

HAL UART wake-up type definition

HAL1

HAL2

typedef struct
{
    uint32_t WakeUpEvent;
    uint16_t AddressLength;
    uint8_t Address;
} UART_WakeUpTypeDef;

In HAL2, the structure has been removed. Each member of the structure is now managed through specific functions. For more details, see the section UART stop mode wake-up source configuration.


HAL UART autonomous mode configuration type definition

HAL1

HAL2

typedef struct
{
    uint32_t TriggerSelection;
    uint32_t TriggerPolarity;
    uint32_t DataSize;
    uint32_t AutonomousModeState;
    uint32_t IdleFrame;
} USART_AutonomousModeConfTypeDef;
typedef struct
{
    hal_uart_am_trig_source_t source;
    hal_uart_am_trig_polarity_t trigger_polarity;
    uint32_t packet_size_byte;
} hal_uart_am_trig_config_t;

Note

AutonomousModeState and IdleFrame have been removed from the structure. These functionalities are now managed through separate functions. For more details, see the section HAL UART autonomous mode.

Related concept: Replaced TypeDef suffix with _t in type names


HAL UART callback type definition

HAL1

HAL2

pUART_CallbackTypeDef

hal_uart_cb_t

pUART_RxEventCallbackTypeDef

hal_uart_rx_cplt_cb_t

Related concept: Replaced TypeDef suffix with _t in type names